//
//  KLJsonToModelTransformer.m
//  KoalaUtilKit
//
//  Created by Tommy on 15/1/8.
//  Copyright (c) 2015年 Koala Team. All rights reserved.
//

#import "KLJsonToModelTransformer.h"
#import "ReactiveCocoa.h"
#import "KLModel.h"
#import "KLLog.h"
#import "KLError.h"

@implementation KLJsonToModelTransformer
+ (KLJsonToModelTransformer*)transformerWithModel:(Class)modelCls{
    KLJsonToModelTransformer *transformer = [[KLJsonToModelTransformer alloc]initWithBlock:^RACSignal *(id<KLIModel> object) {
        NSError *error = nil;
        id result = nil;
        if([modelCls conformsToProtocol:@protocol(KLIModel)]){
            result = [modelCls objectWithJson:object error:&error];
        }else{
            KLLogError(@"%@ not conformsToProtocol KLIModel",modelCls);
            error = [NSError errorWithDomain:KLTransformErrorDomain code:KLTransformErrorCodeUnrecognize userInfo:@{NSLocalizedFailureReasonErrorKey:@"unrecognied class",NSLocalizedDescriptionKey:@"未知错误"}];
        }
        
        if(result){
            return [RACSignal return:result];
        }else{
            return [RACSignal error:error];
        }
        
    }];
    
    return transformer;
}



@end
